﻿
var gid = "GLOBAL_POP_CONTAINER";   // 大容器ID
var maxDialogCount = 4;             // 一个页面最大同时弹出层数量

$(document).ready(function () {
    var topWin = window.top;
    if (topWin.document.getElementById(gid)) { return; }
    //============================================================================================    
    var freeContainer = new Array();    // 空闲的弹出层容器

    var div = topWin.document.createElement("DIV");
    div.id = gid;
    div.style.cssText = "display:none;";
    topWin.document.body.appendChild(div);

    var subdiv = null;
    for (var i = 0; i < maxDialogCount; i++) {
        freeContainer.push("GLOBAL_POP_DIV_2011_08_24_" + i);
        subdiv = topWin.document.createElement("DIV");
        subdiv.innerHTML = "<iframe frameborder='0' width='100%' scroll='auto' height='100%'/>";
        subdiv.id = "GLOBAL_POP_DIV_2011_08_24_" + i;
        div.appendChild(subdiv);
    }
    $(div).data("freeContainer", freeContainer);

    //让对象居中
    jQuery.fn.center = function () {
        this.css("position", "absolute");
        this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
        this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
        return this;
    }

    //加载提示信息div
    var html = '<div class="promptDlg" id="top_promptDlg_210323"></div>';
    $(topWin.document.body).append(html);
    $("#top_promptDlg_210323").center();
    $("#top_promptDlg_210323").css("top", "54px");

    //处理日期控件 在谷歌20以下版本的bug


//    window.setTimeout(function () { $("#_my97DP").hide(); }, 1000);
//    window.setTimeout(function () { $("#_my97DP").hide(); }, 2000);
//    window.setTimeout(function () { $("#_my97DP").hide(); }, 3000);
});

function TopDialog(url, opt) {
    var topWin = window.top;
    if (topWin != window) { return topWin.TopDialog(url, opt); }
    //============================================================================================
    var div = $("#GLOBAL_POP_CONTAINER");
    var freeContainer = div.data("freeContainer");

    if (freeContainer.length < 1) {
        alert("抱歉！ 最多只能同时弹出 " + maxDialogCount + " 个层!");
        return;
    }
    var popdiv = $("#" + freeContainer.pop());
    popdiv.attr("URL", url);

    if (opt.onClose == null) {
        opt.onClose = function (event, ui) {
            freeContainer.push(this.id);
            $("#" + this.id).removeAttr("URL");
            $("#" + this.id + " iframe").attr("src", "");
        }
    }
    else {
        var F = opt.onClose;
        opt.onClose = function (event, ui) {
            F(event, ui);
            freeContainer.push(this.id);
            $("#" + this.id).removeAttr("URL");
            $("#" + this.id + " iframe").attr("src", "");
        }
    }
    if (opt.modal == null) {
        opt.modal = true;
    }
    popdiv.dialog(opt);
    $("iframe", popdiv).attr("src", url);
}

function GlobalData(key, value) {
    var topWin = window.top;
    if (topWin != window) { return topWin.GlobalData(key, value); }
    //============================================================================================
    return $("#" + gid).data(key, value);
}

function GlobalClose(url) {
    var topWin = window.top;
    if (topWin != window) { return topWin.GlobalClose(window.location.href); }
    //============================================================================================
    var divs = $("div[id^='GLOBAL_POP_DIV_2011_08_24_'][URL]");
    for (var i = 0; i < divs.length; i++) {
        var curl = $(divs[i]).attr("URL").split('?')[0];
        if (url.indexOf(curl) > -1) {
            $(divs[i]).dialog("close");
            break;
        }
    }
}

var childWidth = 0;
var childHeight = 0;
function SetChild(cw, ch) {
    childWidth = cw;
    childHeight = ch
}

/*
msg:消息 
obj:对象
*/
var show_msg_timeout;
function show_msg(msg, obj) {
    if (msg == null || msg == "") return;
    var options = {
        msg: '',            //消息
        target:null,        //对象ID
        clickHide:true,      //是否点击消失
        css: 'promptDlg'
    };
    if (typeof (msg) == "object") {
        var options = $.extend({}, options, msg);
        //兼容一下代码参数
        msg = options.msg;
        obj = options.target;
    } else {
        options.msg = msg;
        options.target = obj;
    }

    if ($("#top_promptDlg_210323").is(":visible") == true) {
        $("#top_promptDlg_210323").html("");
        window.clearTimeout(show_msg_timeout);
        $("#top_promptDlg_210323").hide();
    }

    var topWin = window.top;
    if (topWin != window) {
        topWin.SetChild($(window).width(), $(window).height(), window.location.href);
        return topWin.show_msg(options);
        //return topWin.show_msg(msg, obj);
    }

    var time = 3000;
    if (obj != undefined && obj != null) {
        var divh = $("#top_promptDlg_210323").height();
        var divw = $("#top_promptDlg_210323").width();

        var curWidth = 0;
        var curHeight = 0;
        if ($("#" + gid).data("freeContainer").length != maxDialogCount) { //表示页面是通过TopDialog 弹出
            curWidth = (($(window).width() - childWidth) / 2) - 2;
            curHeight = (($(window).height() - childHeight) / 2) + 15;
        } else {
            curWidth = 182;
            curHeight = 120;
        }

        var Ttop = $(obj).offset().top + curHeight; //加上外部高度，即：系统上边区域
        var Twidth = $(obj).offset().left + curWidth; //加上左边的宽度

        $("#top_promptDlg_210323").css("left", Twidth + "px");
        $("#top_promptDlg_210323").css("top", (Ttop + $(obj).outerHeight()) + 'px');

        time = 3000;
        if (options.clickHide) {
            //当点击控件时 消失
            $(obj).bind("click", function () {
                $("#top_promptDlg_210323").hide();
            });
        }else {

            $("#top_promptDlg_210323").mouseenter(function () {
                $("#top_promptDlg_210323").attr("stop", "true");
            });

            $("#top_promptDlg_210323").mouseout(function () {
                $("#top_promptDlg_210323").attr("stop", null);
            });
        }

    } else {
        $("#top_promptDlg_210323").center();
        $("#top_promptDlg_210323").css("top", "24px");
        time = 3000;
    }

    $("#top_promptDlg_210323").removeClass();
    $("#top_promptDlg_210323").addClass(options.css);
    $("#top_promptDlg_210323").show();
    $("#top_promptDlg_210323").html(msg);

    window.clearInterval(show_msg_timeout);
    show_msg_timeout = window.setInterval(function () {
        if ($("#top_promptDlg_210323").attr("stop") == undefined || $("#top_promptDlg_210323").attr("stop") != "true") {
            $("#top_promptDlg_210323").hide();
            window.clearInterval(show_msg_timeout);
        }
    }, time);
}

